package com.zxp.web.sys_menu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zxp.web.VO.ResultUtils;
import com.zxp.web.VO.ResultVO;
import com.zxp.web.constants.Constants;
import com.zxp.web.sys_menu.entity.AssignTreeParam;
import com.zxp.web.sys_menu.entity.AssignTreeVO;
import com.zxp.web.sys_menu.entity.SysMenu;
import com.zxp.web.sys_menu.service.SysMenuService;
import com.zxp.web.sys_user.entity.SysUser;
import com.zxp.web.sys_user.service.SysUserService;
import com.zxp.web.utils.SystemUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @author MasterZ
 * @Date 2025/10/4 09:43
 */
@RestController
@RequestMapping("/api/sysMenu")
public class SysMenuController {
    @Autowired
    private SysMenuService sysMenuService;
    @Autowired
    private SysUserService sysUserService;

    @PostMapping("/add")
    public ResultVO addMenu(@RequestBody SysMenu sysMenu) {
        if (sysMenuService.save(sysMenu)) {
            return ResultUtils.success();
        }
        return ResultUtils.error();
    }

    @PutMapping
    public ResultVO editMenu(@RequestBody SysMenu sysMenu) {
        if (sysMenuService.updateById(sysMenu)) {
            return ResultUtils.success();
        }
        return ResultUtils.error();
    }

    @DeleteMapping("/{menuId}")
    public ResultVO editMenu(@PathVariable Long menuId) {
        //如果存在下级不能删除
        QueryWrapper<SysMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysMenu::getParentId, menuId);
        List<SysMenu> list = sysMenuService.list(queryWrapper);
        if (list != null && !list.isEmpty()) {
            return ResultUtils.error("该菜单存在下级，不能删除！");
        }
        if (sysMenuService.removeById(menuId)) {
            return ResultUtils.success();
        }
        return ResultUtils.error();
    }

    @GetMapping("/getList")
    public ResultVO getList() {
        //排序
        QueryWrapper<SysMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().orderByAsc(SysMenu::getOrderNum);
        List<SysMenu> list = sysMenuService.list(queryWrapper);
        //组装树
        return ResultUtils.success("查询成功！", SystemUtils.toTree(list, Constants.MENU_ROOT_ID));
    }

    //获取上级菜单树,新增菜单时选择上级菜单时使用，只查询目录和菜单，不查询按钮
    @GetMapping("/getParent")
    public ResultVO getParentMenu() {
        List<SysMenu> list = sysMenuService.getParent();
        return ResultUtils.success("查询成功", list);
    }

    //查询菜单树
    @GetMapping("/getAssignTree")
    public ResultVO getAssignTree(AssignTreeParam param) {
        AssignTreeVO assignTree = sysMenuService.getAssignTree(param);
        return ResultUtils.success("查询成功", assignTree);
    }

    /**
     * 查询路由菜单
     *
     * @param userId 用户ID
     * @return 路由菜单
     */
    @GetMapping("/getMenuList")
    public ResultVO getMenuList(Long userId) {
        SysUser user = sysUserService.getById(userId);
        List<SysMenu> menuList;
        //是否超级管理员
        if (StringUtils.isNotEmpty(user.getIsAdmin()) && Constants.IS_ADMIN.equals(user.getIsAdmin())) {
            menuList = sysMenuService.list();
        } else {
            menuList = sysMenuService.getMenuByUserId(userId);
        }
        //过滤菜单数据,去掉按钮
        List<SysMenu> list = Optional.ofNullable(menuList).orElse(new ArrayList<>())
                .stream()
                .filter(item -> item != null && StringUtils.isNotEmpty(item.getType()) && !Constants.MENU_TYPE_BUTTON.equals(item.getType()))
                .toList();
        return ResultUtils.success("查询成功", SystemUtils.toTreeRouter(list, Constants.MENU_ROOT_ID));
    }
}
